<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="平台订单ID" prop="platformOrderId">
        <el-input
          v-model="queryParams.platformOrderId"
          placeholder="请输入平台订单ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="平台订单号" prop="platformOrderNo">
        <el-input
          v-model="queryParams.platformOrderNo"
          placeholder="请输入平台订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="平台编号" prop="platformId">
        <el-input
          v-model="queryParams.platformId"
          placeholder="请输入平台编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="店铺编号" prop="storeId">
        <el-input
          v-model="queryParams.storeId"
          placeholder="请输入店铺编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户编号" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户编号" prop="customerNo">
        <el-input
          v-model="queryParams.customerNo"
          placeholder="请输入客户编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select
          v-model="queryParams.orderType"
          placeholder="请选择订单类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CROSSBORDER_ORDER_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单来源" prop="orderSource">
        <el-input
          v-model="queryParams.orderSource"
          placeholder="请输入订单来源"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="销售渠道" prop="salesChannel">
        <el-input
          v-model="queryParams.salesChannel"
          placeholder="请输入销售渠道"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select
          v-model="queryParams.orderStatus"
          placeholder="请选择订单状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CROSSBORDER_ORDER_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付状态" prop="paymentStatus">
        <el-select
          v-model="queryParams.paymentStatus"
          placeholder="请选择支付状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CROSSBORDER_PAYMENT_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="履约状态" prop="fulfillmentStatus">
        <el-select
          v-model="queryParams.fulfillmentStatus"
          placeholder="请选择履约状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CROSSBORDER_FULFILLMENT_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单货币" prop="currencyCode">
        <el-input
          v-model="queryParams.currencyCode"
          placeholder="请输入订单货币"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="汇率" prop="exchangeRate">
        <el-input
          v-model="queryParams.exchangeRate"
          placeholder="请输入汇率"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="基础货币" prop="baseCurrency">
        <el-input
          v-model="queryParams.baseCurrency"
          placeholder="请输入基础货币"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="商品种类数" prop="itemCount">
        <el-input
          v-model="queryParams.itemCount"
          placeholder="请输入商品种类数"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="商品总数量" prop="totalQuantity">
        <el-input
          v-model="queryParams.totalQuantity"
          placeholder="请输入商品总数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="商品小计金额" prop="subtotalAmount">
        <el-input
          v-model="queryParams.subtotalAmount"
          placeholder="请输入商品小计金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="运费" prop="shippingAmount">
        <el-input
          v-model="queryParams.shippingAmount"
          placeholder="请输入运费"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="税费" prop="taxAmount">
        <el-input
          v-model="queryParams.taxAmount"
          placeholder="请输入税费"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="关税" prop="dutyAmount">
        <el-input
          v-model="queryParams.dutyAmount"
          placeholder="请输入关税"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="保险费" prop="insuranceAmount">
        <el-input
          v-model="queryParams.insuranceAmount"
          placeholder="请输入保险费"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="手续费" prop="handlingFee">
        <el-input
          v-model="queryParams.handlingFee"
          placeholder="请输入手续费"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="折扣金额" prop="discountAmount">
        <el-input
          v-model="queryParams.discountAmount"
          placeholder="请输入折扣金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="优惠券金额" prop="couponAmount">
        <el-input
          v-model="queryParams.couponAmount"
          placeholder="请输入优惠券金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="积分抵扣金额" prop="pointsAmount">
        <el-input
          v-model="queryParams.pointsAmount"
          placeholder="请输入积分抵扣金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="调整金额" prop="adjustmentAmount">
        <el-input
          v-model="queryParams.adjustmentAmount"
          placeholder="请输入调整金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="订单总金额" prop="totalAmount">
        <el-input
          v-model="queryParams.totalAmount"
          placeholder="请输入订单总金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="已支付金额" prop="paidAmount">
        <el-input
          v-model="queryParams.paidAmount"
          placeholder="请输入已支付金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="退款金额" prop="refundAmount">
        <el-input
          v-model="queryParams.refundAmount"
          placeholder="请输入退款金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="获得积分" prop="pointsEarned">
        <el-input
          v-model="queryParams.pointsEarned"
          placeholder="请输入获得积分"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="使用积分" prop="pointsUsed">
        <el-input
          v-model="queryParams.pointsUsed"
          placeholder="请输入使用积分"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="下单时间" prop="orderTime">
        <el-date-picker
          v-model="queryParams.orderTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="确认时间" prop="confirmTime">
        <el-date-picker
          v-model="queryParams.confirmTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="支付时间" prop="paymentTime">
        <el-date-picker
          v-model="queryParams.paymentTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="发货时间" prop="shipTime">
        <el-date-picker
          v-model="queryParams.shipTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="送达时间" prop="deliveryTime">
        <el-date-picker
          v-model="queryParams.deliveryTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="完成时间" prop="completeTime">
        <el-date-picker
          v-model="queryParams.completeTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="取消时间" prop="cancelTime">
        <el-date-picker
          v-model="queryParams.cancelTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="取消原因" prop="cancelReason">
        <el-input
          v-model="queryParams.cancelReason"
          placeholder="请输入取消原因"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="预计送达日期" prop="expectedDeliveryDate">
        <el-date-picker
          v-model="queryParams.expectedDeliveryDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="发货截止时间" prop="deliveryDeadline">
        <el-date-picker
          v-model="queryParams.deliveryDeadline"
          value-format="YYYY-MM-DD"
          type="date"
          placeholder="选择发货截止时间"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="自动确认天数" prop="autoConfirmDays">
        <el-input
          v-model="queryParams.autoConfirmDays"
          placeholder="请输入自动确认天数"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="买家留言" prop="buyerMessage">
        <el-input
          v-model="queryParams.buyerMessage"
          placeholder="请输入买家留言"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="卖家备注" prop="sellerMessage">
        <el-input
          v-model="queryParams.sellerMessage"
          placeholder="请输入卖家备注"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="内部备注" prop="internalNotes">
        <el-input
          v-model="queryParams.internalNotes"
          placeholder="请输入内部备注"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="风险评分" prop="riskScore">
        <el-input
          v-model="queryParams.riskScore"
          placeholder="请输入风险评分"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="风险等级" prop="riskLevel">
        <el-select
          v-model="queryParams.riskLevel"
          placeholder="请选择风险等级"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CROSSBORDER_RISK_LEVEL)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="反欺诈检查状态" prop="fraudCheckStatus">
        <el-select
          v-model="queryParams.fraudCheckStatus"
          placeholder="请选择反欺诈检查状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CROSSBORDER_FRAUD_CHECK_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否紧急订单" prop="isUrgent">
        <el-select
          v-model="queryParams.isUrgent"
          placeholder="请选择是否紧急订单"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否礼品订单" prop="isGift">
        <el-select
          v-model="queryParams.isGift"
          placeholder="请选择是否礼品订单"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="礼品留言" prop="giftMessage">
        <el-input
          v-model="queryParams.giftMessage"
          placeholder="请输入礼品留言"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否代发货" prop="isDropshipping">
        <el-select
          v-model="queryParams.isDropshipping"
          placeholder="请选择是否代发货"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单标签，逗号分隔" prop="tags">
        <el-input
          v-model="queryParams.tags"
          placeholder="请输入订单标签，逗号分隔"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['crossborder:order-main:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['crossborder:order-main:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
            type="danger"
            plain
            :disabled="isEmpty(checkedIds)"
            @click="handleDeleteBatch"
            v-hasPermi="['crossborder:order-main:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleRowCheckboxChange"
    >
    <el-table-column type="selection" width="55" />
      <el-table-column label="订单编号" align="center" prop="id" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="平台订单ID" align="center" prop="platformOrderId" />
      <el-table-column label="平台订单号" align="center" prop="platformOrderNo" />
      <el-table-column label="平台编号" align="center" prop="platformId" />
      <el-table-column label="店铺编号" align="center" prop="storeId" />
      <el-table-column label="客户编号" align="center" prop="customerId" />
      <el-table-column label="客户编号" align="center" prop="customerNo" />
      <el-table-column label="订单类型" align="center" prop="orderType" />
      <el-table-column label="订单来源" align="center" prop="orderSource" />
      <el-table-column label="销售渠道" align="center" prop="salesChannel" />
      <el-table-column label="订单状态" align="center" prop="orderStatus" />
      <el-table-column label="支付状态" align="center" prop="paymentStatus" />
      <el-table-column label="履约状态" align="center" prop="fulfillmentStatus" />
      <el-table-column label="订单货币" align="center" prop="currencyCode" />
      <el-table-column label="汇率" align="center" prop="exchangeRate" />
      <el-table-column label="基础货币" align="center" prop="baseCurrency" />
      <el-table-column label="商品种类数" align="center" prop="itemCount" />
      <el-table-column label="商品总数量" align="center" prop="totalQuantity" />
      <el-table-column label="商品小计金额" align="center" prop="subtotalAmount" />
      <el-table-column label="运费" align="center" prop="shippingAmount" />
      <el-table-column label="税费" align="center" prop="taxAmount" />
      <el-table-column label="关税" align="center" prop="dutyAmount" />
      <el-table-column label="保险费" align="center" prop="insuranceAmount" />
      <el-table-column label="手续费" align="center" prop="handlingFee" />
      <el-table-column label="折扣金额" align="center" prop="discountAmount" />
      <el-table-column label="优惠券金额" align="center" prop="couponAmount" />
      <el-table-column label="积分抵扣金额" align="center" prop="pointsAmount" />
      <el-table-column label="调整金额" align="center" prop="adjustmentAmount" />
      <el-table-column label="订单总金额" align="center" prop="totalAmount" />
      <el-table-column label="已支付金额" align="center" prop="paidAmount" />
      <el-table-column label="退款金额" align="center" prop="refundAmount" />
      <el-table-column label="获得积分" align="center" prop="pointsEarned" />
      <el-table-column label="使用积分" align="center" prop="pointsUsed" />
      <el-table-column
        label="下单时间"
        align="center"
        prop="orderTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="确认时间"
        align="center"
        prop="confirmTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="支付时间"
        align="center"
        prop="paymentTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="发货时间"
        align="center"
        prop="shipTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="送达时间"
        align="center"
        prop="deliveryTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="完成时间"
        align="center"
        prop="completeTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="取消时间"
        align="center"
        prop="cancelTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="取消原因" align="center" prop="cancelReason" />
      <el-table-column label="预计送达日期" align="center" prop="expectedDeliveryDate" />
      <el-table-column
        label="发货截止时间"
        align="center"
        prop="deliveryDeadline"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="自动确认天数" align="center" prop="autoConfirmDays" />
      <el-table-column label="买家留言" align="center" prop="buyerMessage" />
      <el-table-column label="卖家备注" align="center" prop="sellerMessage" />
      <el-table-column label="内部备注" align="center" prop="internalNotes" />
      <el-table-column label="风险评分" align="center" prop="riskScore" />
      <el-table-column label="风险等级" align="center" prop="riskLevel" />
      <el-table-column label="反欺诈检查状态" align="center" prop="fraudCheckStatus" />
      <el-table-column label="是否紧急订单" align="center" prop="isUrgent" />
      <el-table-column label="是否礼品订单" align="center" prop="isGift" />
      <el-table-column label="礼品留言" align="center" prop="giftMessage" />
      <el-table-column label="是否代发货" align="center" prop="isDropshipping" />
      <el-table-column label="订单标签，逗号分隔" align="center" prop="tags" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['crossborder:order-main:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['crossborder:order-main:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <OrderMainForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { OrderMainApi, OrderMain } from '@/api/crossborder/ordermain'
import OrderMainForm from './OrderMainForm.vue'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

/** 跨境电商订单主 列表 */
defineOptions({ name: 'OrderMain' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<OrderMain[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderNo: undefined,
  platformOrderId: undefined,
  platformOrderNo: undefined,
  platformId: undefined,
  storeId: undefined,
  customerId: undefined,
  customerNo: undefined,
  orderType: undefined,
  orderSource: undefined,
  salesChannel: undefined,
  orderStatus: undefined,
  paymentStatus: undefined,
  fulfillmentStatus: undefined,
  currencyCode: undefined,
  exchangeRate: undefined,
  baseCurrency: undefined,
  itemCount: undefined,
  totalQuantity: undefined,
  subtotalAmount: undefined,
  shippingAmount: undefined,
  taxAmount: undefined,
  dutyAmount: undefined,
  insuranceAmount: undefined,
  handlingFee: undefined,
  discountAmount: undefined,
  couponAmount: undefined,
  pointsAmount: undefined,
  adjustmentAmount: undefined,
  totalAmount: undefined,
  paidAmount: undefined,
  refundAmount: undefined,
  pointsEarned: undefined,
  pointsUsed: undefined,
  orderTime: [],
  confirmTime: [],
  paymentTime: [],
  shipTime: [],
  deliveryTime: [],
  completeTime: [],
  cancelTime: [],
  cancelReason: undefined,
  expectedDeliveryDate: [],
  deliveryDeadline: undefined,
  deliveryDeadline: [],
  autoConfirmDays: undefined,
  buyerMessage: undefined,
  sellerMessage: undefined,
  internalNotes: undefined,
  riskScore: undefined,
  riskLevel: undefined,
  fraudCheckStatus: undefined,
  isUrgent: undefined,
  isGift: undefined,
  giftMessage: undefined,
  isDropshipping: undefined,
  tags: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OrderMainApi.getOrderMainPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await OrderMainApi.deleteOrderMain(id)
    message.success(t('common.delSuccess'))
    currentRow.value = {}
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除跨境电商订单主 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await OrderMainApi.deleteOrderMainList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: OrderMain[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await OrderMainApi.exportOrderMain(queryParams)
    download.excel(data, '跨境电商订单主.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>